package com.atlearn.boot_web.util;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;


public class AttendenceUtil {
    public AttendenceUtil() {
    }

    public  String getbinding(String url, String userName, String password) {
        // 获取配置文件内容
        // 获取用户名
        String data_login = "serviceName=clogin&userName="+userName+"&password="+password;
        String login_str;
        String result="";
        try {
            login_str = operate(url,data_login);
            String bxdinfo ="["+login_str+"]";//手动转换成json格式，方便取值
            if(bxdinfo!="[]"){
                JSONArray brand_list = JSONArray.fromObject(bxdinfo);
                if(brand_list!=null){
                    for(int j=0;j<brand_list.size();j++){
                        JSONObject brandInfo = (JSONObject)brand_list.get(j);
                        String Binding=brandInfo.get("binding")==null?"":brandInfo.get("binding")+"";// 获取Binding值
                        String Result=brandInfo.get("result")==null?"":brandInfo.get("result")+"";//获取返回值
                        if("true".equals(Result)){
                            result=Binding;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }

    /**
     * CIP Web 服务应用实例
     * @param url_str CIp Web 服务系统的统一请求地址,现在用的是测试系统的请求地址。
     * @param data_str 业务数据字符串
     * @throws Exception
     */
    public  String operate(String url_str,String data_str){ //连接cloudcc系统(post)
        URL url =null;
        HttpURLConnection con =null;
        OutputStreamWriter out =null;
        InputStream is=null;
        BufferedReader br =null;
        String st = "";
        try{
            // 获取配置文件内容
            url = new URL(url_str);
            con = (HttpURLConnection)url.openConnection();
            con.setRequestMethod("POST"); //post  方式请求
            // 是否接受输出结果
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setConnectTimeout(5000);
            con.setReadTimeout(5000);
            con.connect();//连接
            out = new OutputStreamWriter(con.getOutputStream(),"UTF-8");
            out.write(data_str);
            out.close();
            int responseCode =0;
            if(con!=null){
                responseCode = con.getResponseCode();
            }
            if (responseCode==200){
                is = con.getInputStream();
                br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
                String sc = "";
                while ((sc = br.readLine()) != null) {
                    st = st + sc;
                }
            }else{
            }
        } catch (Exception ex) {
        }finally{
            try {
                if (out != null) {
                    out.close();
                    out = null;
                }
                if (is != null) {
                    is.close();
                    is = null;
                }
                if (br != null) {
                    br.close();
                    br = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return st;
    }
    public  String getURLEncoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }


}

